/**
 * Project Name:HomeActivity
 * Date:2016-3-3上午10:22:42
 * Copyright (c) 2016, askzhao All Rights Reserved.
 *
 */
package com.kkd.kkdapp.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.KKDWebActivity;
import com.kkd.kkdapp.http.entity.HomeRecommend;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.SystemUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * ClassName: RegularAdapter <br/>
 * date: 2016-3-3 上午10:22:42 <br/>
 * 
 * @author askzhao
 * @version
 */
public class IndexFooterListListAdapter extends BaseAdapter {

	private ArrayList<HomeRecommend> mBanners;
	private Context mContext;

	public IndexFooterListListAdapter(Context context,
			ArrayList<HomeRecommend> banners) {
		mContext = context;
		mBanners = banners;

	}

	@Override
	public int getCount() {
		return mBanners.size();
	}

	@Override
	public Object getItem(int position) {

		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		return getBannerView(position, convertView, parent);
	}

	private View getBannerView(final int position, View convertView,
			ViewGroup parent) {
		BannerHolder nBannerHolder;
		if (convertView == null
				|| !(convertView.getTag() instanceof BannerHolder)) {
			nBannerHolder = new BannerHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.index_activity_img, null);
			nBannerHolder.img = (ImageView) convertView
					.findViewById(R.id.image_banner);
			convertView.setTag(nBannerHolder);

		} else {
			nBannerHolder = (BannerHolder) convertView.getTag();
		}
		ImageLoader.getInstance().displayImage(
				mBanners.get(position).getImage(), nBannerHolder.img,
				R.drawable.home_ad);
		nBannerHolder.img.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {

				try {
					if (!TextUtils.isEmpty(mBanners.get(position).getUrl())) {
						SystemUtils.onEvent("Regular_Banner_" + (position + 1),
								1);
						Intent intent = new Intent(mContext,
								KKDWebActivity.class);
						intent.putExtra("useTitleName", true);
						intent.putExtra("titleName", mBanners.get(position)
								.getTitle());
						intent.putExtra("desc", mBanners.get(position)
								.getDesc());
						String url = mBanners.get(position).getUrl();
						intent.putExtra("url", mBanners.get(position).getUrl());
						intent.putExtra("shareUrl", mBanners.get(position)
								.getShareUrl());
						mContext.startActivity(intent);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});
		return convertView;
	}

	private class BannerHolder {
		ImageView img;
	}

}
